/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package l2s.gameserver.handler;

import java.lang.reflect.InvocationTargetException;

import org.reflections.Reflections;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class L2HandlerLoader
{
	private static final Logger _log = LoggerFactory.getLogger(L2HandlerLoader.class.getName());
	
	public static void init()
	{
		try
		{
			Reflections reflections = new Reflections("l2s.gameserver.handler.admin");
			for (final Class<? extends IAdminCommandHandler> handler : reflections.getSubTypesOf(IAdminCommandHandler.class))
			{
				AdminCommandHandler.getInstance().registerHandler(handler.getConstructor().newInstance());
			}
			
			reflections = new Reflections("l2s.gameserver.handler.chat");
			for (final Class<? extends IChatHandler> chatHandler : reflections.getSubTypesOf(IChatHandler.class))
			{
				ChatHandler.getInstance().registerHandler(chatHandler.getConstructor().newInstance());
			}
			
			reflections = new Reflections("l2s.gameserver.handler.item");
			for (final Class<? extends IItemHandler> itemHandler : reflections.getSubTypesOf(IItemHandler.class))
			{
				ItemHandler.getInstance().registerHandler(itemHandler.getConstructor().newInstance());
			}
			
			reflections = new Reflections("l2s.gameserver.handler.skill");
			for (final Class<? extends ISkillHandler> skillHandler : reflections.getSubTypesOf(ISkillHandler.class))
			{
				SkillHandler.getInstance().registerHandler(skillHandler.getConstructor().newInstance());
			}
			
			reflections = new Reflections("l2s.gameserver.handler.user");
			for (final Class<? extends IUserCommandHandler> userCommandHandler : reflections.getSubTypesOf(IUserCommandHandler.class))
			{
				UserCommandHandler.getInstance().registerHandler(userCommandHandler.getConstructor().newInstance());
			}
			
			reflections = new Reflections("l2s.gameserver.handler.voiced");
			for (final Class<? extends IVoicedCommandHandler> voicedCommandHandler : reflections.getSubTypesOf(IVoicedCommandHandler.class))
			{
				VoicedCommandHandler.getInstance().registerHandler(voicedCommandHandler.getConstructor().newInstance());
			}
		}
		catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e)
		{
			_log.warn("Handler {} can't be initialized.", e);
		}
	}
}